package com.fable.common.handler;

import com.alibaba.excel.write.handler.SheetWriteHandler;
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
import com.alibaba.excel.write.metadata.holder.WriteWorkbookHolder;
import lombok.Setter;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;

import java.util.Calendar;

/**
 * @Author : guozhenhu
 * @date: 2023/7/21
 * @Description : excel表头样式设置
 **/

public class ExcelWriteHandler implements SheetWriteHandler {

    /**
     * 附表
     */
    @Setter
    private static String attachedName;
    /**
     * excel名
     */
    @Setter
    private static String excelName;
    /**
     * 黔西南州
     */
    @Setter
    private static String qianXinan;
    /**
     * 填表单位
     */
    @Setter
    private static String formFillingUnit;
    /**
     * 填表人
     */
    @Setter
    private static String preparer;
    /**
     * 联系电话
     */
    @Setter
    private static String contactNumber;
    /**
     * 单位
     */
    @Setter
    private static String unit;

    /**
     * 尾列
     */
    @Setter
    private static int lostCal;
    /**
     * 是否汇总
     */
    @Setter
    private static boolean collect = false;

    @Override
    public void beforeSheetCreate(WriteWorkbookHolder writeWorkbookHolder, WriteSheetHolder writeSheetHolder) {

    }

    @Override
    public void afterSheetCreate(WriteWorkbookHolder writeWorkbookHolder, WriteSheetHolder writeSheetHolder) {

        //获取当前年月
        int year = Calendar.getInstance().get(Calendar.YEAR);
        int month = Calendar.getInstance().get(Calendar.MONTH) + 1;
        String yearMonth = year+"年"+month+"月";

        Workbook workbook = writeWorkbookHolder.getWorkbook();
        Sheet sheet = workbook.getSheetAt(0);
        Row row1 = sheet.createRow(0);
//        row1.setHeight((short) 500);
        Cell cell = row1.createCell(0);
        //设置单元格内容
        cell.setCellValue(attachedName);
        //设置标题
        Row row2 = sheet.createRow(1);
        row2.setHeight((short) (33*20));
        Cell cell1 = row2.createCell(0);
        cell1.setCellValue(qianXinan + yearMonth + excelName);
        CellStyle cellStyle = workbook.createCellStyle();
        cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);//垂直居中
        cellStyle.setAlignment(HorizontalAlignment.CENTER);//水平居中
        Font font = workbook.createFont();
//        font.setBold(true);
        font.setFontName("方正小标宋简体");
//        font.setFontHeight((short) 200);
        font.setFontHeightInPoints((short) 24);
        cellStyle.setFont(font);
        cell1.setCellStyle(cellStyle);
        sheet.addMergedRegionUnsafe(new CellRangeAddress(1, 1, 0, lostCal-1));
        //设置填写栏样式
        Row row3 = sheet.createRow(2);
        row3.setHeight((short) (35*20));
        if (collect) {
            cellType(row3.createCell(1),workbook,formFillingUnit);
            cellType(row3.createCell(6),workbook,preparer);
            cellType(row3.createCell(10),workbook,contactNumber);
            cellType(row3.createCell(20),workbook,unit);
        } else {
            cellType(row3.createCell(1),workbook,formFillingUnit);
            cellType(row3.createCell(4),workbook,preparer);
            cellType(row3.createCell(7),workbook,contactNumber);
            cellType(row3.createCell(12),workbook,unit);
        }

    }

    public void cellType (Cell cell,Workbook workbook,String title){
        CellStyle cellStyle = workbook.createCellStyle();
        Font font = workbook.createFont();
        font.setFontName("楷体_GB2312");
        font.setFontHeightInPoints((short) 14);
        cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);//垂直居中
        cellStyle.setAlignment(HorizontalAlignment.LEFT);//水平靠左
        cellStyle.setFont(font);
        cell.setCellStyle(cellStyle);
        cell.setCellValue(title);
    }
}